package com.whcoding.base.project.common.api;

import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * <p>
 * 分页结果集
 * </p>
 *
 * @author whcoding
 * @date Created in 2021-10-25 23:10
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BasePageResult<T> {
    /**
     * 总条数
     */
    private Long total;

    /**
     * 页码
     */
    private Integer pageNumber;

    /**
     * 每页结果数
     */
    private Integer pageSize;

    /**
     * 结果集
     */
    private List<T> list;

    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> BasePageResult<T> restPage(List<T> list) {
        BasePageResult<T> result = new BasePageResult<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        result.setTotal(Long.valueOf(pageInfo.getPages()));
        result.setPageNumber(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }
}
